
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_CUSTOMER_MEMBER_LIST,
    GET_CUSTOMER_MEMBER_SELECTION,
    CREATE_CUSTOMER_MEMBER,
    EDIT_CUSTOMER_MEMBER,
    DELETE_CUSTOMER_MEMBER,
    GET_CUSTOMER_MEMBER_DETAIL,
    SET_CUSTOMER_MEMBER_DEFAULT,
    GET_CUSTOMER_MEMBER_DISCOUNT,
    CREATE_CUSTOMER_MEMBER_DISCOUNT,
    EDIT_CUSTOMER_MEMBER_DISCOUNT,
    GET_CUSTOMER_LIST,
    GET_CUSTOMER_SUMMERY,
    GET_CUSTOMER_DETAIL,
    GET_CUSTOMER_ACCOUNT_INFO,
    GET_CUSTOMER_CONSUMPTION_INFO,
    GET_CUSTOMER_ORDER_LIST,
    GET_CUSTOMER_BALANCE_LIST,
    GET_CUSTOMER_INTEGRAL_LIST,
    MODIFY_CUSTOMER_CARD,
} from '@constants/ActionTypes'
import { deleteAxios, getAxios, postAxios, putAxios } from '@utils/api'
import { telephoneValidation } from '@utils/regex'

// --- 会员计划 ---

// 获取会员卡列表
export const getCustomerMemberList = (params, callback) => ({
    type: apiType(GET_CUSTOMER_MEMBER_LIST).request,
    params,
    callback
})

// 会员卡下拉(选择)列表
export const getCustomerMemberSelection = (params, callback) => ({
    type: apiType(GET_CUSTOMER_MEMBER_SELECTION).request,
    params,
    callback
})

// 会员卡详情
export const getCustomerMemberDetail = (params, callback) => ({
    type: apiType(GET_CUSTOMER_MEMBER_DETAIL).request,
    params,
    callback
})

// 创建会员卡
export const createCustomerMember = (params, callback) => ({
    type: apiType(CREATE_CUSTOMER_MEMBER).request,
    params,
    callback
})

// 编辑会员卡
export const editCustomerMember = (params, callback) => ({
    type: apiType(EDIT_CUSTOMER_MEMBER).request,
    params,
    callback
})

// 删除会员卡
export const deleteCustomerMember = (params, callback) => ({
    type: apiType(DELETE_CUSTOMER_MEMBER).request,
    params,
    callback
})

// 设置默认会员卡
export const setCustomerMemberDefault = (params, callback) => ({
    type: apiType(SET_CUSTOMER_MEMBER_DEFAULT).request,
    params,
    callback
})

// 获取会员卡折扣信息
export const getCustomerMemberDiscount = (params, callback) => ({
    type: apiType(GET_CUSTOMER_MEMBER_DISCOUNT).request,
    params,
    callback
})

// 新建会员卡折扣信息
export const createCustomerMemberDiscount = (params, callback) => ({
    type: apiType(CREATE_CUSTOMER_MEMBER_DISCOUNT).request,
    params,
    callback
})

// 编辑会员卡折扣信息
export const editCustomerMemberDiscount = (params, callback) => ({
    type: apiType(EDIT_CUSTOMER_MEMBER_DISCOUNT).request,
    params,
    callback
})

// --- 客户查询 ---

// 客户查询列表
export const getCustomerList = ({ card_id, search, page_no, page_size }, callback) => {
    const params = {
        card_id,
        page_no,
        page_size
    }
    search && telephoneValidation.test(search) && (params.mobile = search)
    search && !telephoneValidation.test(search) && (params.nickname = search)
    return {
        type: apiType(GET_CUSTOMER_LIST).request,
        params,
        callback
    }
}

// 客户概况
export const getCustomerSummery = (params, callback) => ({
    type: apiType(GET_CUSTOMER_SUMMERY).request,
    params,
    callback
})

// 客户详情
export const getCustomerDetail = (params, callback) => ({
    type: apiType(GET_CUSTOMER_DETAIL).request,
    params,
    callback
})

// 客户账号详情
export const getCustomerAccountInfo = (params, callback) => ({
    type: apiType(GET_CUSTOMER_ACCOUNT_INFO).request,
    params,
    callback
})

// 客户消费统计详情
export const getCustomerConsumptionInfo = (params, callback) => ({
    type: apiType(GET_CUSTOMER_CONSUMPTION_INFO).request,
    params,
    callback
})

// 客户消费(记录)列表
export const getCustomerOrderList = (params, callback) => ({
    type: apiType(GET_CUSTOMER_ORDER_LIST).request,
    params,
    callback
})

// 客户余额(变动)列表
export const getCustomerBalanceList = (params, callback) => ({
    type: apiType(GET_CUSTOMER_BALANCE_LIST).request,
    params,
    callback
})

// 客户积分(变动)列表
export const getCustomerIntegralList = (params, callback) => ({
    type: apiType(GET_CUSTOMER_INTEGRAL_LIST).request,
    params,
    callback
})

// 客户更换会员卡
export const modifyCustomerCard = (params, callback) => ({
    type: apiType(MODIFY_CUSTOMER_CARD).request,
    params,
    callback
})

const sagas = {
    * [apiType(GET_CUSTOMER_MEMBER_LIST).request]({ params, callback }) {
        try {
            const response = yield getAxios('/member/api/card/v1/list', params)
            yield put({
                type: apiType(GET_CUSTOMER_MEMBER_LIST).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_MEMBER_LIST).failure })
        }
    },
    * [apiType(GET_CUSTOMER_MEMBER_SELECTION).request]({ params, callback }) {
        try {
            const response = yield getAxios('/member/api/card/v1/selection', params)
            yield put({
                type: apiType(GET_CUSTOMER_MEMBER_SELECTION).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_MEMBER_SELECTION).failure })
        }
    },
    * [apiType(GET_CUSTOMER_MEMBER_DETAIL).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/member/api/card/v1/${params.card_id}`)
            yield put({
                type: apiType(GET_CUSTOMER_MEMBER_DETAIL).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_MEMBER_DETAIL).failure })
        }
    },
    * [apiType(CREATE_CUSTOMER_MEMBER).request]({params, callback}) {
        try {
            const response = yield postAxios('/member/api/card/v1/create', params)
            yield put({
                type: apiType(SET_CUSTOMER_MEMBER_DEFAULT).success,
                response,
            })
            callback && callback()
        } catch (e) {
            yield put({type: apiType(SET_CUSTOMER_MEMBER_DEFAULT).failure})
        }
    },
    * [apiType(EDIT_CUSTOMER_MEMBER).request]({params, callback}) {
        try {
            const response = yield putAxios('/member/api/card/v1/update', params)
            yield put({
                type: apiType(SET_CUSTOMER_MEMBER_DEFAULT).success,
                response,
            })
            callback && callback()
        } catch (e) {
            yield put({type: apiType(SET_CUSTOMER_MEMBER_DEFAULT).failure})
        }
    },
    * [apiType(DELETE_CUSTOMER_MEMBER).request]({params, callback}) {
        try {
            const response = yield deleteAxios(`/member/api/card/v1/delete/${params.card_id}`)
            yield put({
                type: apiType(DELETE_CUSTOMER_MEMBER).success,
                response,
            })
            callback && callback()
        } catch (e) {
            yield put({type: apiType(DELETE_CUSTOMER_MEMBER).failure})
        }
    },
    * [apiType(SET_CUSTOMER_MEMBER_DEFAULT).request]({params, callback}) {
        try {
            const response = yield putAxios('/member/api/card/v1/default/set', params)
            yield put({
                type: apiType(SET_CUSTOMER_MEMBER_DEFAULT).success,
                response,
            })
            callback && callback()
        } catch (e) {
            yield put({type: apiType(SET_CUSTOMER_MEMBER_DEFAULT).failure})
        }
    },
    * [apiType(GET_CUSTOMER_MEMBER_DISCOUNT).request]({params, callback}) {
        try {
            const response = yield getAxios(`/market/card_discount/by/${params.card_id}`, null, () => {})
            yield put({
                type: apiType(GET_CUSTOMER_MEMBER_DISCOUNT).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            if (e.status === 404) {
                const response = {}
                yield put({
                    type: apiType(GET_CUSTOMER_MEMBER_DISCOUNT).success,
                    response,
                })
                callback && callback(response)
            } else {
                yield put({type: apiType(GET_CUSTOMER_MEMBER_DISCOUNT).failure})
            }
        }
    },
    * [apiType(CREATE_CUSTOMER_MEMBER_DISCOUNT).request]({params, callback}) {
        try {
            const response = yield postAxios(`/market/card_discount`, params)
            yield put({
                type: apiType(CREATE_CUSTOMER_MEMBER_DISCOUNT).success,
                response,
            })
            callback && callback()
        } catch (e) {
            yield put({type: apiType(CREATE_CUSTOMER_MEMBER_DISCOUNT).failure})
        }
    },
    * [apiType(EDIT_CUSTOMER_MEMBER_DISCOUNT).request]({params, callback}) {
        try {
            const response = yield putAxios(`/market/card_discount`, params)
            yield put({
                type: apiType(EDIT_CUSTOMER_MEMBER_DISCOUNT).success,
                response,
            })
            callback && callback()
        } catch (e) {
            yield put({type: apiType(EDIT_CUSTOMER_MEMBER_DISCOUNT).failure})
        }
    },
    * [apiType(GET_CUSTOMER_LIST).request]({ params, callback }) {
        try {
            const response = yield getAxios('/member/api/v1/consumers/search', params)
            yield put({
                type: apiType(GET_CUSTOMER_LIST).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_LIST).failure })
        }
    },
    * [apiType(GET_CUSTOMER_SUMMERY).request]({ params, callback }) {
        try {
            const response = yield getAxios('/member/api/v1/statistic/summery', params)
            yield put({
                type: apiType(GET_CUSTOMER_SUMMERY).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_SUMMERY).failure })
        }
    },
    * [apiType(GET_CUSTOMER_DETAIL).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/member/api/v1/consumers/${params.member_id}`)
            yield put({
                type: apiType(GET_CUSTOMER_DETAIL).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({type: apiType(GET_CUSTOMER_DETAIL).failure});
        }
    },
    * [apiType(GET_CUSTOMER_ACCOUNT_INFO).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/member/api/v1/consumers/${params.member_id}/account`)
            yield put({
                type: apiType(GET_CUSTOMER_ACCOUNT_INFO).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({type: apiType(GET_CUSTOMER_ACCOUNT_INFO).failure});
        }
    },
    * [apiType(GET_CUSTOMER_CONSUMPTION_INFO).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/member/api/v1/consumers/${params.member_id}/consumption`)
            yield put({
                type: apiType(GET_CUSTOMER_CONSUMPTION_INFO).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({type: apiType(GET_CUSTOMER_CONSUMPTION_INFO).failure});
        }
    },
    * [apiType(GET_CUSTOMER_ORDER_LIST).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/mall/web/status/member-orders`, params)
            yield put({
                type: apiType(GET_CUSTOMER_ORDER_LIST).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_ORDER_LIST).failure })
        }
    },
    * [apiType(GET_CUSTOMER_BALANCE_LIST).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/member/api/v1/consumers/${params.member_id}/balance-logs`, params)
            yield put({
                type: apiType(GET_CUSTOMER_BALANCE_LIST).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_BALANCE_LIST).failure })
        }
    },
    * [apiType(GET_CUSTOMER_INTEGRAL_LIST).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/member/api/v1/consumers/${params.member_id}/integrals`, params)
            yield put({
                type: apiType(GET_CUSTOMER_INTEGRAL_LIST).success,
                response,
            })
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_CUSTOMER_INTEGRAL_LIST).failure })
        }
    },
    * [apiType(MODIFY_CUSTOMER_CARD).request]({params, callback}) {
        try {
            const response = yield putAxios(`/member/api/v1/card/update`, params)
            yield put({
                type: apiType(MODIFY_CUSTOMER_CARD).success,
                response,
            })
            callback && callback()
        } catch (e) {
            yield put({type: apiType(MODIFY_CUSTOMER_CARD).failure})
        }
    },
}

export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})
